package com.heima.behavior.service.impl;

import com.heima.behavior.dto.BehaviorDto;
import com.heima.behavior.entity.ApBehaviorEntry;
import com.heima.behavior.entity.ApUnlikesBehavior;
import com.heima.behavior.service.IApBehaviorEntryService;
import com.heima.behavior.service.IApUnlikesBehaviorService;
import com.heima.common.dto.ResponseResult;
import com.heima.common.enums.AppHttpCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * APP不喜欢行为表 服务实现类
 * </p>
 *
 * @author Wq
 * @since 2022-10-18
 */
@Service
public class ApUnlikesBehaviorServiceImpl implements IApUnlikesBehaviorService {

    @Autowired
    private IApBehaviorEntryService apBehaviorEntryService;
    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 保存用户不喜欢行为
     *
     * @param dto dto
     * @return {@link ResponseResult}
     */
    @Override
    public ResponseResult saveApUnlikes(BehaviorDto dto) {
        //生成EntryId
        ApBehaviorEntry entry = apBehaviorEntryService.getEntryByUserId(dto.getUserId());
        //参数校验
        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //组装数据
        ApUnlikesBehavior apUnlikesBehavior = new ApUnlikesBehavior();
        apUnlikesBehavior.setEntryId(entry.getId());
        apUnlikesBehavior.setArticleId(dto.getArticleId());
        apUnlikesBehavior.setOperation(dto.getOperation());
        apUnlikesBehavior.setCreatedTime(new Date());
        mongoTemplate.save(apUnlikesBehavior);
        return ResponseResult.okResult();

    }
}
